# -*- coding: utf-8 -*-

import numpy as np
import data2.config as config
import cv2
import utils


def mapping_line_parser(line):
    return line.split(',')


def char2one_hot(c):
    one_hot = np.zeros(config.label_size, np.uint8)
    ascii = ord(c)
    if ascii >= 65:

        one_hot[ascii - 55] = 1
    else:
        one_hot[ascii - 48] = 1
    return one_hot


def one_hot2char(one_hot):
    index = np.argmax(one_hot)
    if index > 9:
        return chr(int(index) + 55)
    else:
        return chr(int(index) + 48)


def cut_hack_func(char_image, w, char_image_width):
    if w > 1.5 * char_image_width:
        return np.array_split(char_image, 2, axis=1)
    else:
        return [char_image]


def preprocessing(gray_img):
    # # 中值模糊 3
    # median_blur = 3
    # # 阈值 117
    # threshold = 117
    # normal_image_narray = cv2.medianBlur(gray_img, median_blur)
    # _, normal_image_narray = cv2.threshold(normal_image_narray, threshold, 255, cv2.THRESH_BINARY)
    #
    # return cut_image(normal_image_narray)
    _, normal_image_narray = cv2.threshold(gray_img, 123, 255, cv2.THRESH_BINARY)
    return cut_image(normal_image_narray)


def cut_image(image_narray):
    """
    切割图片
    :param normal_image_narray: 要切割的图片
    :return: 返回单个字符图片列表
    """

    return np.split(image_narray, 5, axis=1)
